package com.qiwo.common.config;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.qiwo.common.domain.ContentDO;
import com.qiwo.common.service.ContentService;
import com.qiwo.common.utils.DateUtils;
import com.qiwo.common.utils.MD5Utils;
import com.qiwo.common.utils.cache.Cache;
import com.qiwo.sys.filter.XssHttpServletRequestWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.xml.crypto.Data;
import java.io.IOException;
import java.util.Date;

/**
 * Copyright
 *
 * @Author:chenwenchao
 * @Date:2018/10/28 0028
 * Description:
 */
@Order(0)
/*@WebFilter(filterName = "sytemFilter", urlPatterns = "/*")*/
public class AuthenticFilter implements Filter {

    @Autowired
    private ContentService contentService;
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void destroy() {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {


       String local= servletRequest.getServerName();
      if(local.equals("localhost")||local.equals("127.0.0.1")){
          filterChain.doFilter(servletRequest,servletResponse);
          return;
      }
      try {


          String url = ((XssHttpServletRequestWrapper) servletRequest).getRequestURI();
          if(url.equals("/")||url.contains("login")||url.contains("authcode")
          ||url.contains("AjaxauthCode")||url.endsWith(".html")||
                  url.startsWith("/other")|| url.startsWith("/static")||url.startsWith("/uploadImg")||
                  url.startsWith("/home")||url.startsWith("/css")||url.endsWith(".js")||url.endsWith(".css")){

              filterChain.doFilter(servletRequest,servletResponse);
              return;
          }
      }catch (Exception ex){
          filterChain.doFilter(servletRequest,servletResponse);
          return;
      }
      if(Cache.get("autoPass")!=null){
          if((boolean)Cache.get("autoPass")){
              filterChain.doFilter(servletRequest,servletResponse);
              return;
          }
      }
        if(Cache.get("AuthCode")==null){
            ContentDO content2 = contentService.findOneByKv("type_id",0);
            if(content2!=null) {
                Cache.add("AuthCode", content2.getContent());
            }
        }
        if(Cache.get("AuthCode")!=null) {
          try {
              String code = Cache.get("AuthCode").toString();
              code = MD5Utils.decodeBase64(code);
              System.out.println(code + "值");
              JSONObject jsonObject = JSON.parseObject(code);
              if (jsonObject.get("urls") != null) {
                  if (jsonObject.get("urls").toString().contains(local)) {
                      if (jsonObject.get("date") != null) {
                          String date = jsonObject.getString("date");
                          if (date.equals("notime")) {
                              filterChain.doFilter(servletRequest, servletResponse);
                              return;
                          } else if (jsonObject.getDate("date") != null) {
                              Date date1 = jsonObject.getDate("date");
                              Date now = new Date();
                              if (now.getTime() <= date1.getTime()) {
                                  filterChain.doFilter(servletRequest, servletResponse);
                                  Cache.add("autoPass", true);
                                  return;
                              }
                          }

                      }
                  }
              }
          }catch (Exception ex){
              System.out.println("报错了"+ex.toString());
          }
        }
      /*  servletResponse.g("/main/login")*/;
        servletRequest.getRequestDispatcher("/main/authcode").forward(servletRequest,servletResponse);
    }
//
//    @Override
//    public void init(FilterConfig filterConfig) throws ServletException {
//
//    }
//
//    @Override
//    public void destroy() {
//
//    }
}
